package com.tangkeyon.controller.manage;

import com.tangkeyon.common.Const;
import com.tangkeyon.common.ResponseCode;
import com.tangkeyon.pojo.User;
import com.tangkeyon.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Author: tangkeyon@gmail.com
 * @Date: 2019/5/25 11:15
 * @Version 1.0
 */
@Controller
@RequestMapping("/manage/user")
public class UserManagerController {
    @Autowired
    private IUserService iUserService;


    @RequestMapping(value="login.do",method = RequestMethod.POST)
    @ResponseBody
    public ResponseCode<User> login(String username, String password, HttpSession session) {
//        response内包含返回的数据和返回状态的描述，因此每一层的处理都要将Response返回给前端
        ResponseCode<User> response = iUserService.login(username, password);
        if (response.ifSuccess()) {
            User user = response.getData();
            if (user.getRole() == Const.Role.ROLE_ADMIN) {
                session.setAttribute(Const.CURRENT_USER, user);
                return response;
            }
                return ResponseCode.createByErrorMessage("不是管理员无法登录");
        }
                return response;//返回的是登录错误的信息
    }

}
